* report demand and supply parameter estimates

* Figure 2: own-price elasticity

* Figure 3: WTP for mpg  and hp

* Figure 4: valuation ratio

* Table 3: marginal costs, fuel economy shadow cost, markup by firm

* Table 4: Trade-off and marginal cost parameters

* Table 5: Fuel economy technology costs

* Appendix Figure 2: predicted and observed attributes by group

* Appendix Figure 3: comparison of no-change and demand model forecasts

* Appendix Table 7: Valuation ratio by income group allowing for within-group heterogeneity


clear all
set more off
set type double
set seed 123456
log using parameter_estimates_summary, replace text

* macro for demographic group
global demo = "income_group old urban"

* macro for vehicle identifier
global vehicle = "make model series drive_type fuel_type body_style liter"

* macro for share of sales in zev states
global zev_share = 0.5

* macro for number of households for appendix table 7
global hhobs = 10

* macro for fractional cost-per-mile change
global delta = 0.01

***********
* Figures 2-4
***********

* load market data
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Data\supply_estimation_results", clear

* keep 2018
keep if market_ID==2018

* compute utility from outside good consistent wth market shares
egen demo = group($demo)
egen vehicle = group($vehicle)
sort demo market_ID vehicle
by demo market_ID: egen pr_mkt_share = sum(exp(util_new))
by demo market_ID: egen sumnew = sum(sales)
gen used_share = used_sales/(used_sales + sumnew)
replace util_used = used_share*pr_mkt_share/(1 - used_share)
replace util_used = ln(util_used)

* predict market share
replace pr_mkt_share = exp(util_new)/(exp(util_used) + pr_mkt_share)
replace pr_mkt_share = 0 if pr_mkt_share==.

* compute predicted sales
gen pr_sales = mkt_size*pr_mkt_share

* predict share in new
sort $demo market_ID vehicle
by $demo market_ID: egen pr_new_share = sum(exp(util_new))
replace pr_new_share = exp(util_new)/pr_new_share

* compute own price elasticity
gen own_p = alpha*trans_price*(1 - pr_new_share)

* compute wtp for 1 percent fuel economy change
global delta = 0.01
gen wtp_fe = - beta_cpm*(cpm*(1/(1 + $delta)) - cpm)/alpha

* compute wtp for 1 percent horsepower change
gen ln_hw = ln(hp/weight)
gen wtp_hp = - beta_hw*(ln((1 + $delta)*hp/weight) - ln_hw)/alpha

* compute valuation ratio
gen vr = wtp_fe/((cpm - cpm*(1/(1 + $delta)))*sum_vmt_disc_3)

* compute means by income group
sort income_group market_ID demo vehicle
by income_group: egen wt = sum(pr_sales)
replace wt = pr_sales/wt
foreach kind in own_p wtp_fe wtp_hp vr {
    by income_group: egen demo_`kind' = sum(wt*`kind')
}

* collapse data set
sort income_group
drop if income_group==income_group[_n-1]
keep income_group demo_own_p demo_wtp_fe demo_wtp_hp demo_vr
sort income_group
export excel using "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\figures2_3_4", firstrow(variables) replace


***********
* Table 3: marginal costs, fuel economy shadow cost, markup by firm
***********

* load market data
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Data\supply_estimation_results", clear

* keep 2018
keep if market_ID==2018

* compute ZEV subsidy (assume fraction of sales in zev states)
gen zev_req = 0.045
gen zev_credit = 0 if fuel_type=="Electric" & fuel_type!="Plug-in Hybrid" & market_ID>=2015
replace zev_credit = 0.01*evrange + 0.5 if fuel_type=="Electric" & market_ID>=2015
replace zev_credit = 4 if fuel_type=="Electric" & evrange>=350 & market_ID>=2015
replace zev_credit = 0.01*evrange + 0.3 if fuel_type=="Plug-in Hybrid" & market_ID>=2015
replace zev_credit = 1.1 if fuel_type=="Plug-in Hybrid" & evrange>=80 & market_ID>=2015
replace zev_credit = 0 if zev_credit==.
gen zev_subsidy = $zev_share*zev_price*(zev_credit - zev_req)

* CAFE credit multipliers
gen electric = fuel_type=="Electric"
gen phev = fuel_type=="Plug-in Hybrid"
gen pev = electric + phev
gen multiplier = 1
replace multiplier = 2 if electric==1
replace multiplier = 1.6 if phev==1

* compute adjusted fuel consumption rate
gen fcr_adj = 1/mpg
replace fcr_adj = 0 if fuel_type=="Electric"
replace fcr_adj = 0.5*fcr_adj if fuel_type=="Plug-in Hybrid"

* compute CAFE feebate
gen feebate = cafe_price*(fcr_adj - 1/standard)*multiplier

* compute markup
gen markup = 100*(trans_price + zev_subsidy - feebate - mc)/trans_price

* rank firms by 2018 sales
drop if firm=="Other"
sort firm
by firm: egen sales2018 = sum(sales)
replace sales2018 = . if firm==firm[_n-1]
egen firm_rank = rank(sales2018), field
replace firm_rank = firm_rank[_n-1] if firm_rank==. & firm==firm[_n-1]

* predict market share and sales
egen demo = group($demo)
egen vehicle = group($vehicle)
sort demo vehicle
by demo: egen pr_mkt_share = sum(exp(util_new))
replace pr_mkt_share = exp(util_new)/(exp(util_used) + pr_mkt_share)
replace pr_mkt_share = 0 if pr_mkt_share==.
gen pr_sales = pr_mkt_share*mkt_size


* compute means of marginal costs, feebate, and markup
sort firm $demo $vehicle market_ID
by firm: egen wt = sum(pr_sales)
replace wt = pr_sales/wt
foreach kind in mc feebate markup {
    by firm: egen firm_`kind' = sum(wt*`kind')
}

* collapse
sort firm $demo $vehicle market_ID
drop if firm==firm[_n-1]
keep firm firm_*
sort firm_rank
drop firm_rank

* save
export excel using "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\table3", replace firstrow(variables)

***********
* Table 4: Trade-off and marginal cost parameters
***********

*****
* trade-off parameters
*****

* load market data
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Data\supply_estimation_results", clear


* keep one observation per vehicle
egen demo = group($demo)
egen vehicle = group($vehicle)
sort vehicle market_ID demo
drop if vehicle==vehicle[_n-1] & market_ID==market_ID[_n-1]

* log fuel economy, horsepower, height, and area
gen ln_mpg = ln(mpg)
gen ln_hp = ln(hp)
gen ln_height = ln(height)
gen ln_area = ln(width*length)

* year-model-trim-fuel type-drive type-body style
egen ymsfdb = group(market_ID make model series fuel_type drive_type body_style)

* estimation: regress horsepower on fuel economy and report reciprocal
areg ln_hp ln_mpg ln_height ln_area if truck==0, absorb(ymsfdb) robust
nlcom 1/[#1]ln_mpg
areg ln_hp ln_mpg ln_height ln_area if truck==1, absorb(ymsfdb) robust
nlcom 1/[#1]ln_mpg

*****
* marginal cost parameters
*****

* load data
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Data\supply_estimation_results", clear

* compute log of marginal costs and technology index
gen ln_mc = ln(mc)
gen T = ln(mpg) - tradeoff_hp*ln(hp/weight)

* create year fixed effects and identifiers 
xi i.market_ID
egen demo = group($demo)
egen vehicle = group($vehicle)
egen vm = group(vehicle market_ID)

* variable for converting coefficient to change in marginal costs per mpg
gen ratio = mc/mpg

* estimation
sort vm demo
areg ln_mc T _Imarket* if vm!=vm[_n-1] & truck==0, absorb(vehicle) cluster(vehicle)
summ ratio if e(sample)
global ratio = r(mean)
lincom $ratio*[#1]T
areg ln_mc T _Imarket* if vm!=vm[_n-1] & truck==1, absorb(vehicle) cluster(vehicle)
summ ratio if e(sample)
global ratio = r(mean)
lincom $ratio*[#1]T

***********
* Table 5: Fuel economy technology costs
***********

* load data
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Data\supply_estimation_results", clear

* group and vehicle identifiers
egen demo = group($demo)
egen vehicle = group($vehicle)

* predict market share and sales			
sort demo market_ID vehicle
by demo market_ID: egen pr_mkt_share = sum(exp(util_new))
replace pr_mkt_share = exp(util_new)/(exp(util_used) + pr_mkt_share)
replace pr_mkt_share = 0 if pr_mkt_share==.
gen pr_sales = pr_mkt_share*mkt_size

* compute sales by model
sort make model market_ID
by make model market_ID: egen mpr_sales = sum(pr_sales)

* create make/model ID variable
egen mdl = group(make model)

* report sales-weighted change in marginal costs and average fixed costs for fuel economy changes
gen del_mc_1 = mc*exp(mc_fe*ln(1.01)) - mc
gen del_mc_5 = mc*exp(mc_fe*ln(1.05)) - mc
gen del_mc_10 = mc*exp(mc_fe*ln(1.1)) - mc
summ del_mc_* [aw=pr_sales], detail
gen del_afc_1 = (gamma_m*(0.01)^2)/mpr_sales
gen del_afc_5 = (gamma_m*(0.05)^2)/mpr_sales
gen del_afc_10 = (gamma_m*(0.1)^2)/mpr_sales
sort market_ID mdl demo vehicle
sort market_ID mdl vehicle demo
summ del_afc_* [aw=mpr_sales] if mdl!=mdl[_n-1], detail

***********
* Appendix Figure 1: predicted and observed attributes by group
***********

* load data
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Data\supply_estimation_results", clear

* log horsepower-weight
gen ln_hw = ln(hp/weight)

* compute observed attributes by year and demographic group and year
egen demo = group($demo)
egen vehicle = group($vehicle)
sort market_ID demo vehicle
by market_ID demo: egen obs_wt = sum(sales)
replace obs_wt = sales/obs_wt
foreach kind in trans_price cpm ln_hw footprint {
	by market_ID demo: egen obs_`kind' = sum(obs_wt*`kind')
}

* compute predicted market share
sort $demo market_ID vehicle
by $demo market_ID: egen pr_mkt_share = sum(exp(util_new))
replace pr_mkt_share = exp(util_new)/(exp(util_used) + pr_mkt_share)
replace pr_mkt_share = 0 if pr_mkt_share==.

* compute predicted attributes by year and demographic group and year
sort market_ID demo vehicle
by market_ID demo: egen pr_wt = sum(pr_mkt_share)
replace pr_wt = pr_mkt_share/pr_wt
foreach kind in trans_price cpm ln_hw footprint {
	by market_ID demo: egen pr_`kind' = sum(pr_wt*`kind')
}

* save for plotting
sort market_ID demo vehicle
drop if market_ID==market_ID[_n-1] & demo==demo[_n-1]
keep market_ID demo obs_* pr_*
drop obs_wt pr_mkt_share pr_wt 
export excel using "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\app_fig1", replace firstrow(variables)

***********
* Appendix Figure 2: comparison of no-change and demand model forecasts
***********

* load data
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Data\supply_estimation_results", clear

* group and vehicle identifiers
egen demo = group($demo)
egen vehicle = group($vehicle)

* brand by class identifier
egen brand_class = group(make truck)

* by year and demographic group, total sales
sort market_ID demo
by market_ID demo: egen agg_sales = sum(sales)

* by year: brand-class-group share
sort market_ID demo brand_class
by market_ID demo brand_class: egen obs_share = sum(sales)
replace obs_share = obs_share/agg_sales

* predict market share by vehicle and demographic group
sort $demo market_ID vehicle
by $demo market_ID: egen pr_mkt_share = sum(exp(util_new))
replace pr_mkt_share = exp(util_new)/(exp(util_used) + pr_mkt_share)
replace pr_mkt_share = 0 if pr_mkt_share==.

* by year: brand-class-group share, including only new
sort market_ID demo brand_class
by market_ID demo: egen sum_pred = sum(pr_mkt_share)
by market_ID demo brand_class: egen pred_share = sum(pr_mkt_share)
replace pred_share = pred_share/sum_pred

* collapse data set to year by brand-class-demo
sort market_ID brand_class demo
drop if market_ID==market_ID & brand_class==brand_class[_n-1] & demo==demo[_n-1]
keep market_ID brand_class demo make truck $demo obs_share pred_share
keep if market_ID==2010 | market_ID==2018
reshape wide obs_share pred_share, i(brand_class demo) j(market_ID)
drop pred_share2010
rename pred_share2018 pred_share
rename obs_share2010 no_change
rename obs_share2018 obs_share

* save
export excel using "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\app_fig2", replace firstrow(variables)

***********
* Appendix Table 7: Valuation ratio by income group allowing for within-group heterogeneity
***********

*****
* obtain parameter estimates and cost-per-mile by demographic group and region
*****

* load market data
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Data\supply_estimation_results", clear

* keep 2018
keep if market_ID==2018

* predict market share
egen demo = group($demo)
egen vehicle = group($vehicle)
sort demo market_ID vehicle
by demo market_ID: egen pr_mkt_share = sum(exp(util_new))
replace pr_mkt_share = exp(util_new)/(exp(util_used) + pr_mkt_share)
replace pr_mkt_share = 0 if pr_mkt_share==.

* compute predicted sales
gen pr_sales = mkt_size*pr_mkt_share

* keep parameters and sales by demographic group and region
sort $demo truck vehicle
keep $demo vehicle truck alpha beta_cpm cpm pr_sales

* save
save temp, replace

*****
* by demographic group and class, compute discounted VMT
*****

***
* by demographic group, create sample of households with random APR and ratio of VMT to mean VMT
***

* load data
use "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Data\apr_mean_sd_by_market_demo", clear

* keep 2018
keep if market_ID==2018

* harmonize demographic variables
gen old = young==0
gen income_group = 1 if hhinc=="<44k"
replace income_group = 2 if hhinc=="44-91k"
replace income_group = 3 if hhinc=="91-123k"
replace income_group = 4 if hhinc=="123-185k"
replace income_group = 5 if hhinc==">185k"

* drop extra variables
keep $demo apr_mean apr_sd miles_mean miles_sd corr_apr_miles

* create variable to index households
qui for X in num 1/$hhobs: gen obsX = 1

* reshape to long, by group and household number
reshape long obs, i($demo) j(hh)
drop obs

* sample from APR and VMT distributions (uncorrelated)
gen apr_uncorr = rnormal(apr_mean, apr_sd)
gen vmt_uncorr = rnormal(miles_mean, miles_sd)

* using correlation from data, loop over demographic groups
egen demo = group($demo)
summ demo
global max = r(max)
gen apr_poscorr = .
gen vmt_poscorr = .
gen apr_negcorr = .
gen vmt_negcorr = .
forvalues num = 1/$max {
	summ corr_apr_miles if demo==`num'
	global poscorr = r(mean)
	global negcorr = -$poscorr
	matrix C = (1, $poscorr \ $poscorr, 1)
	summ apr_mean if demo==`num'
	global apr_mean = r(mean)
	summ miles_mean if demo==`num'
	global miles_mean = r(mean)
	summ apr_sd if demo==`num'
	global apr_sd = r(mean)
	summ miles_sd if demo==`num'
	global miles_sd = r(mean)
	drawnorm temp_a temp_v, means($apr_mean, $miles_mean) sds($apr_sd, $miles_sd) corr(C)
	replace apr_poscorr = temp_a if demo==`num'
	replace vmt_poscorr = temp_v if demo==`num'
	drop temp_a temp_v
	matrix C = (1, $negcorr \ $negcorr, 1)	
	drawnorm temp_a temp_v, means($apr_mean, $miles_mean) sds($apr_sd, $miles_sd) corr(C)
	replace apr_negcorr = temp_a if demo==`num'
	replace vmt_negcorr = temp_v if demo==`num'	
	drop temp_a temp_v
}

* set negatives to zero
foreach kind in uncorr poscorr negcorr {
	replace apr_`kind' = 0 if apr_`kind'<0
	replace vmt_`kind' = 0 if vmt_`kind'<0
}
	
* report correlations
foreach kind in uncorr poscorr negcorr {
	corr apr_`kind' vmt_`kind'
}

* compute ratio of VMT to mean VMT
foreach kind in uncorr poscorr negcorr {
	replace vmt_`kind' = vmt_`kind'/miles_mean
}

* drop extra variables and reshape
keep $demo *_uncorr *_poscorr *_negcorr hh
reshape wide apr_uncorr vmt_uncorr apr_poscorr vmt_poscorr apr_negcorr vmt_negcorr, i($demo) j(hh)

* save
sort $demo
save temp2, replace

***
* merge mean VMT and scrappage by age, class, and group 
***

* read in car vmt
insheet using "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Data\vmt_cars.csv", clear 
rename vehage age

* save
save temp3, replace

* read in truck vmt and append to cars
insheet using "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Data\vmt_trucks.csv", clear 
append using temp3

* dummy for trucks
gen truck = vehicle_type=="Light Truck"

* dummy for old
gen oldpos = strpos(hh_age_income, "Age >= 45")
gen old = oldpos>0

* dummy for urban
gen urbpos = strpos(hh_age_income, "urban")
gen urban = urbpos>0

* income categories
gen inc1pos = strpos(hh_age_income, "< $48k")
gen inc2pos = strpos(hh_age_income, "$48k -")
gen inc3pos = strpos(hh_age_income, "$96k -")
gen inc4pos = strpos(hh_age_income, "$120k -")
gen inc5pos = strpos(hh_age_income, "> $193k")
gen income_group = 1 if inc1pos>0
for X in num 2/5: replace income_group = X if incXpos>0

* keep variables and save
keep $demo truck age vmt
drop if vmt<=0 | vmt==.
sort truck age $demo
save temp3, replace

* read in car survival rates
insheet using "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Data\Predicted Survival Rates cars.csv", clear 
keep age predictedsurvivalrate
gen truck = 0

* save
save temp4, replace

* read in truck survival rates
insheet using "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Data\Predicted Survival Rates trucks.csv", clear 
keep age predictedsurvivalrate
gen truck = 1

* append trucks and cars
append using temp4

* merge scrappage with VMT
sort truck age
merge 1:m truck age using temp3
keep if _merge==3
drop _merge

* merge with household sample
sort $demo truck age
merge m:1 $demo using temp2
keep if _merge==3
drop _merge

***
* compute discounted VMT
***

* reshape to long
reshape long apr_uncorr vmt_uncorr apr_poscorr vmt_poscorr apr_negcorr vmt_negcorr, i($demo truck age) j(hh)

* multiply VMT by ratio of household's VMT to mean VMT
foreach kind in uncorr poscorr negcorr {
	replace vmt_`kind' = vmt_`kind'*vmt
}
drop vmt
summ vmt_* apr_*, detail
summ vmt_* apr_* if income_group==5, detail

* compute discounted VMT
foreach kind in uncorr poscorr negcorr {
	gen vmt_disc_`kind' = (vmt_`kind'*predictedsurvivalrate)/(1 + (apr_`kind' - 2)/100)^(age - 1)
}

* compute sum of discounted VMT
sort $demo truck hh age
foreach kind in uncorr poscorr negcorr {
	by $demo truck hh: egen sum_vmt_disc_`kind' = sum(vmt_disc_`kind')
}
sort income_group
by income_group: summ sum_vmt_disc_*

* collapse data set
egen demo_group = group($demo)
sort demo_group truck hh age
drop if demo_group==demo_group[_n-1] & truck==truck[_n-1] & hh==hh[_n-1]
keep $demo truck hh sum_vmt_disc*

* reshape to wide 
reshape wide sum_vmt_disc_uncorr sum_vmt_disc_poscorr sum_vmt_disc_negcorr, i($demo truck) j(hh)

*****
* compute valuation ratio by income group
*****

* merge discouned VMT with preference parameters
sort $demo truck
merge 1:m $demo truck using temp

* reshape to long
reshape long sum_vmt_disc_uncorr sum_vmt_disc_poscorr sum_vmt_disc_negcorr, i($demo truck vehicle) j(hh)

* compute willingness to pay for higher fuel economy
gen wtp_fe = - beta_cpm*(cpm*(1/(1 + $delta)) - cpm)/alpha

* compute valuation ratio
foreach kind in uncorr poscorr negcorr {
	gen vr_`kind' = wtp_fe/((cpm - cpm*(1/(1 + $delta)))*sum_vmt_disc_`kind')
}

* compute mean across households and collapse by vehicle and income group
sort $demo vehicle hh
foreach kind in uncorr poscorr negcorr {
	by $demo vehicle: egen mean_vr_`kind' = mean(vr_`kind')
}
egen demo = group($demo)
sort demo vehicle hh
drop if demo==demo[_n-1] & vehicle==vehicle[_n-1]

* compute sales-weighted valuation ratio by income group
sort income_group vehicle old urban
by income_group: egen wt = sum(pr_sales)
replace wt = pr_sales/wt
foreach kind in uncorr poscorr negcorr {
	drop vr_`kind'
	by income_group: egen vr_`kind' = sum(wt*mean_vr_`kind')
}

*****
* collapse and save
*****

sort income_group
drop if income_group==income_group[_n-1] 
keep income_group vr_*
sort income_group
export excel using "L:\Project-MaritzCX\Workspace1\Public\CAFE Ex Post Replication\Replication Results\app_table7", replace firstrow(variables)

log close